// Fill out your copyright notice in the Description page of Project Settings.

#include "OneLife.h"
#include "Components/StaticMeshComponent.h"


AOneLife::AOneLife() 
{
	tick = false;
}

void AOneLife::Tick(float DeltaSeconds) 
{
	Super::Tick(DeltaSeconds);
	tick = !tick;
	if (tick) 
	{
		UpDate();
	}
	else 
	{
		lifeBuffer = isLife;
	}

}

void AOneLife::UpDate()
{
	adjacentNum = 0;
	for (AOneLife* one : adjacent) 
	{
		if (one->lifeBuffer)
		{
			adjacentNum++;
		}
	}

	if (adjacentNum == 3)
	{
		isLife = true;
	}
	else
	{
		if (adjacentNum == 2)
		{

		}
		else
		{
			isLife = false;

		}
	}
	ShowBody();


}

void AOneLife::ShowBody()
{	
	GetStaticMeshComponent()->SetWorldScale3D(isLife ? FVector(0.9, 0.9, 0.9) : FVector(0.2, 0.2, 0.2));
	//GetStaticMeshComponent()->SetVisibility(isLife, false);
}

void AOneLife::ChangeLife() 
{
	if (adjacentNum == 3)
	{
		isLife = true;
	}
	else 
	{
		if (adjacentNum == 2)
		{

		}
		else
		{
			isLife = false;
		
		}
	}
}

void AOneLife::Init(bool b)
{
	lifeBuffer = isLife = b;
	ShowBody();
}

void AOneLife::InitAdjacentAndPoint(FVector point)
{
	SetActorLocation(point);
}

void AOneLife::AddOneAdjacent(AOneLife* adjacent_) 
{
	adjacent.Add(adjacent_);
}